<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>注册管理</span>
          <el-button type="primary" @click="handleRegister">
            <el-icon><UserFilled /></el-icon>
            注册账号
          </el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="queryParams.username"
            placeholder="请输入用户名"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="注册状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="注册状态" clearable style="width: 200px">
            <el-option label="已注册" value="registered" />
            <el-option label="未注册" value="unregistered" />
            <el-option label="注册失败" value="failed" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮区域 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" @click="handleRegister">
            <el-icon><UserFilled /></el-icon>
            注册账号
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" @click="handleRefresh">
            <el-icon><Refresh /></el-icon>
            刷新状态
          </el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="registerList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户名" align="center" prop="username" />
        <el-table-column label="SIP地址" align="center" prop="sipAddress" />
        <el-table-column label="注册状态" align="center" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="注册时间" align="center" prop="registerTime" width="180" />
        <el-table-column label="过期时间" align="center" prop="expireTime" width="180" />
        <el-table-column label="最后心跳" align="center" prop="lastHeartbeat" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button 
              v-if="scope.row.status === 'registered'"
              type="text" 
              @click="handleUnregister(scope.row)"
            >
              <el-icon><User /></el-icon>
              注销
            </el-button>
            <el-button 
              v-else
              type="text" 
              @click="handleRegister(scope.row)"
            >
              <el-icon><UserFilled /></el-icon>
              注册
            </el-button>
            <el-button type="text" @click="handleDetail(scope.row)">
              <el-icon><View /></el-icon>
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!-- 注册对话框 -->
    <el-dialog title="注册账号" v-model="registerDialogVisible" width="500px" append-to-body>
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="SIP服务器" prop="server">
          <el-input v-model="registerForm.server" placeholder="请输入SIP服务器地址" />
        </el-form-item>
        <el-form-item label="端口" prop="port">
          <el-input-number v-model="registerForm.port" :min="1" :max="65535" />
        </el-form-item>
        <el-form-item label="注册超时" prop="timeout">
          <el-input-number v-model="registerForm.timeout" :min="30" :max="3600" />
          <span style="margin-left: 10px">秒</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitRegister" :loading="registerLoading">
            注册
          </el-button>
          <el-button @click="registerDialogVisible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 遮罩层
const loading = ref(false)
// 选中数组
const ids = ref<string[]>([])
// 显示搜索条件
const showSearch = ref(true)
// 总条数
const total = ref(0)
// 注册列表数据
const registerList = ref<any[]>([])
// 注册对话框
const registerDialogVisible = ref(false)
// 注册加载状态
const registerLoading = ref(false)

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  username: '',
  status: ''
})

// 注册表单
const registerForm = reactive({
  username: '',
  password: '',
  server: '',
  port: 5060,
  timeout: 3600
})

// 表单校验规则
const registerRules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' }
  ],
  server: [
    { required: true, message: 'SIP服务器不能为空', trigger: 'blur' }
  ]
}

// 查询注册列表
const getList = () => {
  loading.value = true
  // 模拟数据
  setTimeout(() => {
    registerList.value = [
      {
        username: '1001',
        sipAddress: 'sip:1001@192.168.1.100',
        status: 'registered',
        registerTime: '2023-12-01 10:00:00',
        expireTime: '2023-12-01 11:00:00',
        lastHeartbeat: '2023-12-01 10:55:00'
      },
      {
        username: '1002',
        sipAddress: 'sip:1002@192.168.1.100',
        status: 'unregistered',
        registerTime: null,
        expireTime: null,
        lastHeartbeat: null
      }
    ]
    total.value = 2
    loading.value = false
  }, 1000)
}

// 获取状态类型
const getStatusType = (status: string) => {
  switch (status) {
    case 'registered':
      return 'success'
    case 'unregistered':
      return 'info'
    case 'failed':
      return 'danger'
    default:
      return 'info'
  }
}

// 获取状态文本
const getStatusText = (status: string) => {
  switch (status) {
    case 'registered':
      return '已注册'
    case 'unregistered':
      return '未注册'
    case 'failed':
      return '注册失败'
    default:
      return '未知'
  }
}

// 搜索按钮操作
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置按钮操作
const resetQuery = () => {
  queryParams.username = ''
  queryParams.status = ''
  handleQuery()
}

// 多选框选中数据
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map(item => item.username)
}

// 注册账号
const handleRegister = (row?: any) => {
  if (row) {
    registerForm.username = row.username
    registerForm.password = ''
    registerForm.server = '192.168.1.100'
    registerForm.port = 5060
    registerForm.timeout = 3600
  } else {
    registerForm.username = ''
    registerForm.password = ''
    registerForm.server = '192.168.1.100'
    registerForm.port = 5060
    registerForm.timeout = 3600
  }
  registerDialogVisible.value = true
}

// 提交注册
const submitRegister = () => {
  registerLoading.value = true
  setTimeout(() => {
    ElMessage.success('注册成功')
    registerDialogVisible.value = false
    registerLoading.value = false
    getList()
  }, 1000)
}

// 注销注册
const handleUnregister = (row: any) => {
  ElMessageBox.confirm('确认注销该账号?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('注销成功')
    getList()
  })
}

// 刷新状态
const handleRefresh = () => {
  ElMessage.success('状态刷新成功')
  getList()
}

// 查看详情
const handleDetail = (row: any) => {
  ElMessage.info('查看注册详情: ' + row.username)
}

// 分页大小改变
const handleSizeChange = (val: number) => {
  queryParams.pageSize = val
  getList()
}

// 页码改变
const handleCurrentChange = (val: number) => {
  queryParams.pageNum = val
  getList()
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.mb8 {
  margin-bottom: 8px;
}

.dialog-footer {
  text-align: right;
}
</style>
